<?php
namespace app\modules\supplier\logic;
use app\modules\base\LogicBase;
use think\Db;


class SupplierLogic extends LogicBase
{
    //验证
    protected $valid;

    public function _initialize()
    {
        $this->valid = validate('app\modules\supplier\validate\SupplierPost'); //游客验证
    }

    /**
     * 获取供应商信息
     * @param integer $id 参数
     * @return bool|array
     */
    public function get($id = null){
        if (empty($id) || !Db::name('supplier')->where(['supplier_id'=>$id])->find()){
            $this->_error[] ='无效参数';
            return false;
        }

        $data = Db::name('supplier')->where(['supplier_id'=>$id])->find();
        return $data;

    }
    /**
     * 添加供应商申请
     * @param integer $id ID
     * @param array $data 参数
     * @return bool|array
     */
    public function edit($id=null,$data=[]){
        if (empty($id) || !Db::name('supplier')->where(['supplier_id'=>$id])->find()){
            $this->_error[] ='无效参数';
            return false;
        }

        if (!$this->valid->check($data)){
            $this->_error[] = $this->valid->getError();
            return false;
        }else{
            if (Db::name('supplier')->where(['supplier_id'=>['neq',$id],'supplier_name'=>$data['supplier_name']])->find()){
                  $this->_error[]='供应商已存在';
                  return false;
            }
        }

        $res = Db::name('supplier')->where(['supplier_id'=>$id])->update($data);

        if ($res!==false){
            return true;
        }else{
            return false;
        }

    }
}